#include "llib_version.h"
#define L_VERSION   1120104
#if L_VERSION == LLIB_VERSION_TO_TEST

#include <boost/test/unit_test.hpp>
using namespace boost;

#include <llib.h>
using namespace llib;
using namespace llib::geom;

#include <vector>
using namespace std;

BOOST_AUTO_TEST_SUITE(BVTree_test_suite)

BOOST_AUTO_TEST_CASE(BVTree_test_case_01) {
    const Int32 vertexNum = 16;
    vector<Vec3f> vertexList(vertexNum);
    Vec3f vertexList0[vertexNum] = { Vec3f( 5.f, 7.f, 0.f), Vec3f( 5.f, 6.f, 0.f), Vec3f( 5.f, 4.f, 0.f), Vec3f( 5.f, 3.f, 0.f),
                                     Vec3f(-5.f, 7.f, 0.f), Vec3f(-5.f, 6.f, 0.f), Vec3f(-5.f, 4.f, 0.f), Vec3f(-5.f, 3.f, 0.f),
                                     Vec3f( 5.f,-7.f, 0.f), Vec3f( 5.f,-6.f, 0.f), Vec3f( 5.f,-4.f, 0.f), Vec3f( 5.f,-3.f, 0.f),
                                     Vec3f(-5.f,-7.f, 0.f), Vec3f(-5.f,-6.f, 0.f), Vec3f(-5.f,-4.f, 0.f), Vec3f(-5.f,-3.f, 0.f) };

    BVTree tree;
    tree.Build(vector<Vec3f>(vertexList0,vertexList0+vertexNum));

    BOOST_CHECK_EQUAL( tree.GetNodeNum(), 31 );
    BOOST_CHECK_EQUAL( tree.GetLeafNum(), 16 );

    BVNode* node = tree.GetRoot();
    BOOST_CHECK( node->IsRoot() );
    BOOST_CHECK( !node->IsLeaf() );

    BVNode* node2 = node->GetChildLeft()->GetChildLeft()->GetChildLeft();
    BOOST_CHECK( !node2->IsRoot() );
    BOOST_CHECK( !node2->IsLeaf() );

    BVNode* leaf1 = node2->GetChildLeft();
    BVNode* leaf2 = node2->GetChildRight();
    BOOST_CHECK( leaf1->IsLeaf() );
    BOOST_CHECK( leaf2->IsLeaf() );
    BOOST_CHECK( abs(leaf1->GetElemIndex() - leaf2->GetElemIndex()) == 1 );
    BOOST_CHECK_EQUAL( leaf1->GetParent(), leaf2->GetParent() );

    node2 = node->GetChildRight()->GetChildLeft()->GetChildLeft();
    BOOST_CHECK( !node2->IsRoot() );
    BOOST_CHECK( !node2->IsLeaf() );

    leaf1 = node2->GetChildLeft();
    leaf2 = node2->GetChildRight();
    BOOST_CHECK( leaf1->IsLeaf() );
    BOOST_CHECK( leaf2->IsLeaf() );
    BOOST_CHECK( abs(leaf1->GetElemIndex() - leaf2->GetElemIndex()) == 1 );
    BOOST_CHECK_EQUAL( leaf1->GetParent(), leaf2->GetParent() );
    BOOST_CHECK_EQUAL( leaf1->GetParent(), node2 );


}

BOOST_AUTO_TEST_SUITE_END()

#endif
